﻿using HighDiveJudge;
using HighDiveJudge.Resources;
using NUnit.Framework;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;

namespace UnitTestHighDiveJudge
{
    [TestFixture]
    class UnitTestJudgeApp
    {
        private JudgeApp _app;
        private string _ipAdress;
        private int _port;
        private string _idString;

        private Dive _d1;
        private Dive _d2;
        private Dive _d3;
        private Dive _d4;
        private Dive _d5;

        private Diver _max;

        [SetUp]
        public void Setup()
        {
            _app = new JudgeApp();
            _ipAdress = "127.0.0.0";
            _port = 9059;
            _idString = "xyz12";

            _max = new Diver{IdNumber = "890716", FirstName = "Max", LastName = "Sevon", Gender = Gender.Male};

            _d1 = new Dive{Diver = _max, DiveCode = "404B", DiveHeight = 10, DiveNumber = 1};
            _d2 = new Dive { Diver = _max, DiveCode = "100A", DiveHeight = 10, DiveNumber = 1 };
            _d3 = new Dive { Diver = _max, DiveCode = "5226D", DiveHeight = 10, DiveNumber = 1 };
            _d4 = new Dive { Diver = _max, DiveCode = "304b", DiveHeight = 10, DiveNumber = 1 };
            _d5 = new Dive { Diver = _max, DiveCode = "200A", DiveHeight = 10, DiveNumber = 1 };

            _app.CurrentDive = _d1;
            _app.DiveHistory.Add(_d2);
            _app.DiveHistory.Add(_d3);
            _app.DiveHistory.Add(_d4);
        }

        [Test]
        public void TestLogin()
        {
            _app.Login(_idString, _ipAdress, _port);
            Assert.AreEqual(true, _app.Connected);
        }

        [Test]
        public void TestUpdateScore()
        {
            //test update without login
            Assert.Throws(typeof (JudgeAppException), () => { _app.UpdateScore(_d1, 10); });

            _app.Login(_idString, _ipAdress, _port);

            //update score in history
            _app.UpdateScore(_d2, 10);
            Assert.AreEqual(10, _app.DiveHistory[0].DiveScoreList[0].Score);

            //update score on CurrentDive
            _app.UpdateScore(_d1, 20);
            Assert.AreEqual(20, _app.DiveHistory[0].DiveScoreList[0].Score);
        }

        [Test]
        public void TestReceiveNewDive()
        {
            Assert.AreEqual(_d1, _app.CurrentDive);

            _app.NewDiveReceived(_d5);
            Assert.AreEqual(_d5, _app.CurrentDive);
            Assert.AreEqual(_d1, _app.DiveHistory[0]);
            Assert.AreEqual(3, _app.DiveHistory.Count);
        }

        [Test]
        public void TestReceiveDiveScore()
        {
            Assert.AreEqual(0, _app.CurrentDive.DiveScoreList.Count);
            
            _app.NewDiveScoreReceived(new DiveScore{Score = 3}, _app.CurrentDive);
            Assert.AreEqual(null, _app.CurrentDive);
            Assert.AreEqual(3, _app.DiveHistory[0].DiveScoreList[0].Score);
        }
    }
}
